﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DrSoftsFrame.Servers;
using System.Data.SqlClient;
using DrSoftsFrame;

namespace PreeRegisterDAL
{
    public class WorkGetDAL
    {
        public static int GetPreeDay(int iWorkPlanID)
        {
            SqlCommand cmd = new SqlCommand();
            string strSql =string.Format(@"declare @ipreeday int

                                            select @ipreeday=ipreeday from OPDWorkPlanType
                                            where iWorkPlanTypeID={0}

                                            if @ipreeday is null
                                            begin
                                              select @ipreeday=cValue from pubSystemSet
                                              where cSystemCode='YYDaySpan'
                                            end

                                            if @ipreeday is null
                                            begin
                                              select @ipreeday=7
                                            end

                                            select @ipreeday", iWorkPlanID);
            ClinicDb mydb = new ClinicDb();
            DataSet ds;
            ds = mydb.ExecuteSelect(strSql);
            if (ds == null || ds.Tables.Count < 1||ds.Tables[0].Rows.Count<1)
            {
                return 7;
            }
            return Common.ToInt(ds.Tables[0].Rows[0][0]);
        }

        /// <summary>
        /// 增加预约来源ID传入  liujia151214
        /// </summary>
        /// <param name="docid"></param>
        /// <param name="ideptid"></param>
        /// <param name="ipreeday"></param>
        /// <param name="IFTmkyy"></param>
        /// <param name="preetype"></param>
        /// <returns></returns>
        public static DataSet GetWork(int docid, int ideptid, int ipreeday, bool IFTmkyy, int preetype)
        {
            ClinicDb mydb = new ClinicDb();
            try
            {
                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = "up_GetWorkPlan";
                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter p1 = new SqlParameter("@idoctorid", SqlDbType.Int);
                p1.Direction = ParameterDirection.Input;
                p1.Value = docid;
                cmd.Parameters.Add(p1);

                SqlParameter p2 = new SqlParameter("@ideptid ", SqlDbType.Int);
                p2.Direction = ParameterDirection.Input;
                p2.Value = ideptid;
                cmd.Parameters.Add(p2);

                SqlParameter p3 = new SqlParameter("@ipreeday", SqlDbType.Int);
                p3.Direction = ParameterDirection.Input;
                p3.Value = ipreeday;
                cmd.Parameters.Add(p3);

                SqlParameter p4 = new SqlParameter("@IFTmkyy", SqlDbType.Bit);
                p4.Direction = ParameterDirection.Input;
                p4.Value = IFTmkyy;
                cmd.Parameters.Add(p4);

                SqlParameter p5 = new SqlParameter("@preetype", SqlDbType.Int);
                p5.Direction = ParameterDirection.Input;
                p5.Value = preetype;
                cmd.Parameters.Add(p5);

                DataSet ds = mydb.ExecuteSqlCmdFillDs(cmd);
                if (ds == null || ds.Tables.Count < 1)
                {
                    return null;
                }
                ds.Tables[0].TableName = "WorkPlan";
                return ds;
            }
            catch (Exception err)
            {
                throw err;
            }
        }

        public static DataSet GetWorkDetail(string iWorkPlanID, bool IFTmkyy)
        {
            SqlCommand cmd = new SqlCommand();
            string strSql = "";            
            if (IFTmkyy)
            {
                //同煤语句去掉上下午时间段限制  liujia151103
                strSql = string.Format(@"	
                                        select a.iDetailId,a.iWorkPlanID,a.iNum
	                                    ,stime=convert(varchar(5),a.dStartTime,108)+'-'+convert(varchar(5),a.dEndTime,108)
	                                    ,istatus=
	                                    case 
	                                    when isnull(a.bclinicflag,0)=1 OR isnull(a.bdiagflag,0)=1 or isnull(a.buseflag,0)=1 then 1 
	                                    else 0 
	                                    end 
	                                    from opdworkplandetail a
	                                    join OPDWorkPlan b on a.iWorkPlanID=b.iWorkPlanID
	                                    where a.iNum>isnull(b.preenbegin,0) 
	                                    --and (CONVERT(varchar(100),a.dStartTime, 8)>'07:00:00' and CONVERT(varchar(100),a.dStartTime, 8)<'12:00:00'
	                                    --or  CONVERT(varchar(100),a.dStartTime, 8)>'14:00:00' and CONVERT(varchar(100),a.dStartTime, 8)<'18:00:00') 
	                                    and a.dStartTime>GETDATE() and a.iWorkPlanID={0} and a.iDoctorId not in (2067,2071,2095,2194,2272,2275) and  a.iDeptId<>49", iWorkPlanID);
            }
            else
            {
                strSql = string.Format(@"	
                                        select iDetailId,iWorkPlanID,iNum
	                                    ,stime=convert(varchar(5),dStartTime,108)+'-'+convert(varchar(5),dEndTime,108)
	                                    ,istatus=
	                                    case 
	                                    when isnull(bPreRegister,0)=1 OR isnull(bCheck,0)=1 or isnull(bLock,0)=1 then 1 
	                                    else 0 
	                                    end 
	                                    from opdworkplandetail
	                                    where iWorkPlanID={0} and dStartTime>GETDATE()", iWorkPlanID);
            }

            ClinicDb mydb = new ClinicDb();
            DataSet ds;
            ds = mydb.ExecuteSelect(strSql);
            if (ds == null || ds.Tables.Count < 1)
            {
                return null;
            }
            return ds;
        }

        public static string GetWorkPlanID(string doctor,string date,string time )
        {
            DateTime dt=DrSoftsFrame.Common.ToDateTime(date);
            string ctimetype = "";
            if (time == "上午")
            {
                ctimetype = "10";
            }
            if (time == "下午")
            {
                ctimetype = "30";
            }
            if (time == "全天")
            {
                ctimetype = "50";
            }

            string sql = string.Format(@"select iworkplanid from opdworkplan where ipersonid={0} and cTimeTypeCode= '{1}' and  dstarttime between '{2} 0:00' and '{2} 23:00'", doctor, ctimetype,dt.ToShortDateString());
            ClinicDb mydb = new ClinicDb();
            DataSet ds = mydb.ExecuteSelect(sql);
            string iworkplanid = ds.Tables[0].Rows[0][0].ToString();
            return iworkplanid;
        }
    }
}
